<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/serve/show' }">车展管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{$router.currentRoute.name}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>

      <el-form-item label="车展时间" prop="startTime">
        <el-col :span="11">
          <el-date-picker v-model="ruleForm.startTime" type="datetime" placeholder="开始日期"
                          default-time="00:00:00"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          style="width: 100%;">
          </el-date-picker>
        </el-col>
        <el-col style="color: #2c3e50;text-align: center" :span="2">——</el-col>
        <el-col :span="11">
          <el-form-item prop="endTime">
            <el-date-picker v-model="ruleForm.endTime" type="datetime" placeholder="结束日期"
                            default-time="23:59:59" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="车展票价" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="购票开始时间" prop="buyTime">
        <el-col :span="11">
          <el-date-picker v-model="ruleForm.buyTime" type="datetime" placeholder="选择日期"
                          default-time="00:00:00" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
          </el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="退票截止时间" prop="returnTime">
        <el-col :span="11">
          <el-date-picker v-model="ruleForm.returnTime" type="datetime" placeholder="选择日期"
                          default-time="23:59:59" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
          </el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            active-color="#13ce66"
            inactive-color="#ccc"
            active-value="1"
            inactive-value="0">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import global from '../../../Global'
let preShowUrl = global.preShows ;

export default {
  data() {
    return {
      ruleForm: {
        enable: '1'
      },
      rules: {
        title: [
          { required: true, message: '请输入车展标题', trigger: 'blur' },
          { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请输入车展开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请输入车展结束时间', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入车展票价', trigger: 'blur' }
        ],
        buyTime: [
          { required: true, message: '请输入购票开始时间', trigger: 'change' }
        ],
        returnTime: [
          { required: true, message: '请输入退票截止时间', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    //新增
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = preShowUrl + "/add-new";
          let formData = this.qs.stringify(this.ruleForm);

          let localJwt = localStorage.getItem('localJwt');
          this.axios.create({'headers': {'Authorization': localJwt}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if(jsonResult.state == 20000){
              this.$notify.success({title: '成功',
                message: '添加成功'
              });

              this.resetForm(formName);
            }else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
